package com.wanxi.dao.Impl;

import com.wanxi.dao.NewsTypeDao;
import com.wanxi.model.NewsTypeModel;
import com.wanxi.util.JDBC;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Li
 * @Date 2023/5/7 20:45
 */
public class NewsTypeDaoImpl implements NewsTypeDao {

    public int add(NewsTypeModel newsTypeModel) {
        String sql = " insert into news_type (`new_type`,`create_time`,`update_time`,`enable`,`remark`) " +
                "value ('" + newsTypeModel.getNewType() + "',now(),sysdate(),'" + newsTypeModel.getEnable() + "','" + newsTypeModel.getRemark() + "');";
        return JDBC.update(sql);
    }

    public int update(NewsTypeModel newsTypeModel) {
        String sql = "update news_type set `new_type`= '" + newsTypeModel.getNewType() + "',`create_time`= now(),`enable`='" + newsTypeModel.getEnable() + "',`remark`='" + newsTypeModel.getRemark() + "' " +
                " where (`id`='" + newsTypeModel.getId() + "') ";
        return JDBC.update(sql);
    }

    public int delete(NewsTypeModel newsTypeModel) {
        String sql = "delete from news_type where id='" + newsTypeModel.getId() + "' ";
        return JDBC.update(sql);
    }

    public List<NewsTypeModel> findAll(NewsTypeModel newsTypeModel) {
        int start = (newsTypeModel.getPage()-1)*newsTypeModel.getLimit();
        String sql = "select * from news_type where true";
        sql += getWhereSql(newsTypeModel);
        sql += " order by id desc";
        sql += " limit "+start+","+newsTypeModel.getLimit()+" ";
        ResultSet query = JDBC.query(sql);
        List<NewsTypeModel> newsTypeModelList = new ArrayList<NewsTypeModel>();
        try {
            while (query.next()) {
                newsTypeModelList.add(NewsTypeModel.builder()
                        .id(query.getInt("id"))
                        .newType(query.getString("new_type"))
                        .remark(query.getString("remark"))
                        .enable(query.getInt("enable"))
                        .build());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return newsTypeModelList;
    }


    private String getWhereSql(NewsTypeModel newsTypeModel) {
        String sql = "";
        if (!"".equals(newsTypeModel.getNewType())) {
            sql += " and new_type like '%" + newsTypeModel.getNewType() + "%'";
        }
        if (newsTypeModel.getEnable() != -1) {
            sql += " and enable = '" + newsTypeModel.getEnable() + "' ";
        }
        return sql;
    }

    /**
     * 根据id查询
     *
     * @param newsTypeModel
     * @return
     */
    public NewsTypeModel findById(NewsTypeModel newsTypeModel) {
        String sql = " select * from news_type where id = '" + newsTypeModel.getId() + "'";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return NewsTypeModel.builder()
                        .id(query.getInt("id"))
                        .newType(query.getString("new_type"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .build();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer count(NewsTypeModel newsTypeModel) {
        String sql = "select count(*) as count from news_type where true";
        if (!"".equals(newsTypeModel.getNewType())) {
            sql += " and new_type like '%" + newsTypeModel.getNewType() + "%'";
        }
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return query.getInt("count");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer totalCount() {
        return null;
    }

    public boolean findSameName(NewsTypeModel newsTypeModel) {
        String sql = "select * from news_type where new_type = '" + newsTypeModel.getNewType() + "' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
